// 用户信息
import {
	defineStore
} from 'pinia';

export const useUserInfoStore = defineStore('useInfoToken', {
	// 定义状态的内容
	state: () => ({
		nickName: "",
		userId: "",
		isAdmin: false,
		avatar: "",
		tokenInfo: "",

	}),

	actions: {
		//保存用户的登录信息
		saveUserInfo(userInfo) {
			this.userId = userInfo.userId;
			this.nickName = userInfo.nickName;
			this.isAdmin = userInfo.isAdmin;
			this.avatar = userInfo.avatar;
			this.tokenInfo = userInfo.tokenInfo;

			// 本地缓存用户信息
			uni.setStorage({
				key: 'userInfo',
				data: userInfo,
			});
		},
		// 删除用户的登录信息
		delUserInfo() {
			this.userId = "";
			this.nickName = "";
			this.isAdmin = false;
			this.avatar = "";
			this.tokenInfo = "";


			// 删除本地缓存
			uni.removeStorage({
				key: 'userInfo',
			});
		},
		// 清理本地所有缓存数据
		clearAllStorage() {
			uni.clearStorageSync();
		},
	},
	// 持久化存储
	// #ifdef H5
	persist: true,
	// #endif

	// #ifdef MP-WEIXIN

	persist: {
		storage: {
			setItem: (key, value) => uni.setStorageSync(key, value),
			getItem: (key) => uni.getStorageSync(key),
		}
	}
	// #endif

});